namespace MoreLinq;

using System;
using System.Collections.Generic;

static partial class MoreEnumerable
{
    static T[] Fold<T>(this IEnumerable<T> source, int count)
    {
            var elements = new T[count];
            var i = 0;

            foreach (var item in source)
            {
                elements[i] = i < count ? item : throw LengthError(1);
                i++;
            }

            if (i < count)
                throw LengthError(-1);

            return elements;

            InvalidOperationException LengthError(int cmp) => new(FormatSequenceLengthErrorMessage(cmp, count));
        }
}
